/**
* @file qologin.cpp
* @describe 登录窗口
* @author gaoyuelong
* @date 2021/11/23
*/

#include "qologin.h"
#include "ui_qologin.h"
#include "qoregister.h"
#include "network/qotcp.h"
#include "common.h"
#include "customwidget/loading.h"
#include "qousermanager.h"
#include <QMessageBox>
#include <QJsonArray>
#include <QVector>
#include <QJsonDocument>

QoLogin::QoLogin(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::QoLogin)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
    m_pQoControl = new QoControl();

    connect(m_pQoControl, SIGNAL(signalLogin(E_ERROR_TYPE, const QJsonObject &)),
            this, SLOT(slotLogin(E_ERROR_TYPE, const QJsonObject &)));
}

QoLogin::~QoLogin()
{
    delete m_pQoControl;
    m_pQoControl = nullptr;
    delete ui;
}

void QoLogin::on_btnLogin_clicked()
{
    //检查账号密码是否为空
    if (ui->lineeditAccount->text().isEmpty()) {
        ui->lineeditAccount->setStyleSheet("border-bottom:1px  solid red;");
        return;
    } else {
        ui->lineeditAccount->setStyleSheet("border-bottom:1px  solid;");
    }
    if (ui->lineeditPassword->text().isEmpty()) {
        ui->lineeditPassword->setStyleSheet("border-bottom:1px  solid red;");
        return;
    } else {
        ui->lineeditPassword->setStyleSheet("border-bottom:1px  solid;");
    }

//    Loading *loading = new Loading();
//    loading->setGeometry(geometry());
//    loading->raise();
//    loading->exec();

    //交给control层去做
    m_pQoControl->login(ui->lineeditAccount->text(), ui->lineeditPassword->text());

//    if (!QoTcp::getInstance()->isConnected()) {
//        //再尝试连接一下
//        QoTcp::getInstance()->connectToServer();
//    }

//    if (QoTcp::getInstance()->isConnected() && login()) {
//        accept();
//    } else {
//        QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("连接服务器失败！"), QMessageBox::Ok);
//        return;
//    }

    return;
}

void QoLogin::on_btnRegister_clicked()
{
    QoRegister *pQoRegister = new QoRegister(this);
    pQoRegister->exec();
}

void QoLogin::on_btnErWeiMa_clicked()
{
    QDialog dialog(this);
    dialog.setFixedSize(220, 220);
    QVBoxLayout *pVlayout = new QVBoxLayout();
    QHBoxLayout *pHlayout = new QHBoxLayout();

    QLabel label;
    label.setFixedSize(200, 200);
    label.setLayout(pHlayout);
    label.setPixmap(QPixmap(":/new/prefix/images/defaultPhoto/me.jpg"));
    pVlayout->addWidget(&label);
    dialog.setLayout(pVlayout);
    dialog.exec();
}

void QoLogin::slotLogin(E_ERROR_TYPE eErrorType, const QJsonObject &jsonRecvMsgObj)
{
    switch (eErrorType){
    case eNoError:{
        accept();
        //登录成功填充该用户的好友信息

        //这里应该构造对象进行set，而不是直接get设置
        QoUserManager::getInstance()->getCurrentUser()->setNickName(jsonRecvMsgObj["NickName"].toString());
        QoUserManager::getInstance()->getCurrentUser()->setState(jsonRecvMsgObj["userstate"].toString());


        QJsonArray jsonFriends = jsonRecvMsgObj["friends"].toArray();

        for (auto jsonFriend : jsonFriends) {
            QoUser qoUser;
            QJsonObject jsonObj = QJsonDocument::fromJson(jsonFriend.toString().toUtf8()).object();
            qoUser.setAccount(jsonObj["friendAccount"].toString());
            qoUser.setNickName(jsonObj["friendNickName"].toString());
            qoUser.setState(jsonObj["state"].toString());
            QoUserManager::getInstance()->getCurrentUserFriends().push_back(qoUser);
        }
    }break;
    case eConnectionFail:{
        return;
    }break;
    }
}
